import api from '../../utils/api'

Page({
    data: {
        categories: [],
        prices: [],
        activeCategory: 0,
        notices: []
    },

    onLoad() {
        this.loadData()
    },

    loadData() {
        // 获取品类
        api.getCategories().then(res => {
            this.setData({ categories: res.data })
            this.loadPrices()
        })

        // 获取价格公告
        api.getPriceNotices().then(res => {
            this.setData({ notices: res.data })
        })
    },

    loadPrices() {
        const { categories, activeCategory } = this.data
        const categoryId = categories[activeCategory]?.id

        api.getPrices(categoryId).then(res => {
            this.setData({ prices: res.data })
        })
    },

    handleCategoryChange(e) {
        this.setData({
            activeCategory: e.detail.name
        }, () => {
            this.loadPrices()
        })
    },

    navigateToOrder() {
        wx.navigateTo({
            url: '/pages/order/order'
        })
    }
})